package com.zmm.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmm.weblog.common.domain.dos.CategoryDo;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.Objects;

/**
 * @Author:福禄寿
 * @Date:2024/4/30 下午4:04
 * @Des:weblog-springboot
 **/
@Mapper
public interface CategoryMapper extends BaseMapper<CategoryDo> {
    /**
     * 根据用户名查询
     */
    default CategoryDo selectByName(String categoryName){
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryDo::getName,categoryName);
        return selectOne(wrapper);
    }

    /**
     * 分页查询
     */
    default Page<CategoryDo> selectPageList(Long current, Long size, String name, LocalDate startDate, LocalDate endDate){
        //分页对象(查询第几页、每页多少数据)
        Page<CategoryDo> page = new Page<>(current, size);

        //构建查询条件
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), CategoryDo::getName, name.trim())
                .ge(Objects.nonNull(startDate), CategoryDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), CategoryDo::getCreateTime, endDate)
                .orderByDesc(CategoryDo::getCreateTime);

        return selectPage(page, wrapper);

    }
}

